package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import Connection.Retriever;


public class DodajUprawnienieRefInputFrame extends JFrame implements ActionListener{


	/**
	 * 
	 */
	private static final long serialVersionUID = -2150983029061097746L;
	JTextField text1, text2, text3;
	JLabel label1, label2, label3;
	JComboBox listaTypow, listaObiektow, listaPodmiotow;
	ArrayList<String> choiceStr1, choiceStr2, choiceStr3;
	String t, o, p;
	DefaultComboBoxModel listaTypowM, listaObiektowM, listaPodmiotowM;
	Retriever listaTypowR, listaObiektowR, listaPodmiotowR;
	
	
	
	public DodajUprawnienieRefInputFrame() throws HeadlessException {
		super("Dodaj uprawnienie referencyjne");
	}
	
	public void start()
	
    {
		label1 = new JLabel("Typ uprawnienia: ");
	    label2 = new JLabel("Obiekt");
	    label3 = new JLabel("Podmiot");
	    
	    ArrayList<String> choiceStr1 = new ArrayList<String>();
	    ArrayList<String> choiceStr2 = new ArrayList<String>();
	    ArrayList<String> choiceStr3 = new ArrayList<String>();
	    
	    listaTypowR = new Retriever();
	    listaObiektowR = new Retriever();
	    listaPodmiotowR = new Retriever();
	    
	    choiceStr1 = listaTypowR.get("TYP_UPRAWNIENIA");
	    choiceStr2 = listaObiektowR.get("OBIEKT");
	    choiceStr3 = listaPodmiotowR.get("PODMIOT");
	    
	    listaTypowM = new javax.swing.DefaultComboBoxModel(choiceStr1.toArray());
	    listaObiektowM = new javax.swing.DefaultComboBoxModel(choiceStr2.toArray());
	    listaPodmiotowM = new javax.swing.DefaultComboBoxModel(choiceStr3.toArray());

	    listaTypow = new JComboBox(choiceStr1.toArray());
	    listaTypow.setModel(listaTypowM);
	    listaTypow.setSelectedIndex(0);
	    listaTypow.addActionListener(this);
	  
	    listaObiektow = new JComboBox(choiceStr2.toArray());
	    listaObiektow.setModel(listaObiektowM);
	    listaObiektow.setSelectedIndex(0);
	    listaObiektow.addActionListener(this);
	  
	    listaPodmiotow = new JComboBox(choiceStr3.toArray());
	    listaPodmiotow.setModel(listaPodmiotowM);
	    listaPodmiotow.setSelectedIndex(0);
	    listaPodmiotow.addActionListener(this);

	    JButton button1 = new JButton("Dodaj");
	    //JButton button2 = new JButton("Zamknij");
	    JButton button3 = new JButton("Dodaj typ");

	    this.add(label1, BorderLayout.WEST);
	    this.add(listaTypow, BorderLayout.EAST);
	    this.add(label2, BorderLayout.WEST);
	    this.add(listaObiektow, BorderLayout.EAST);
	    this.add(label3, BorderLayout.WEST);
	    this.add(listaPodmiotow, BorderLayout.EAST);

	    this.add(button1);
	   // this.add(button2);
	    this.add(button3);
	    
	    button1.addActionListener(this );
	    //button2.addActionListener(this );
	    button3.addActionListener(this );
		
	     setLayout(new FlowLayout());
	     setSize(500,100);
	     setVisible(true);
	     Toolkit kit = Toolkit.getDefaultToolkit();
	     Dimension screenSize = kit.getScreenSize();
	     setLocation((screenSize.width-500)/2, (screenSize.height-100)/2);
	     setDefaultCloseOperation(HIDE_ON_CLOSE);

    }

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand() == "Zamknij"){
			this.dispose();
		}
		else if(e.getActionCommand() == "Dodaj"){
			try {
			     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			     Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=ABSI", "Admin","Admin");
			     CallableStatement statement = conn.prepareCall("EXEC DodajUprawnienieRef ?,?,?"); 
			     	t = (String)listaTypow.getSelectedItem();
			     	o = (String)listaObiektow.getSelectedItem();
			     	p = (String) listaPodmiotow.getSelectedItem();
			     
			     statement.setString(1,t);
			     statement.setString(2,o);
			     statement.setString(3,p);
			     statement.execute();

			     conn.close();
			  } catch (Exception ex) {
			     ex.printStackTrace();
			  }
		}
		else if(e.getActionCommand() == "Dodaj typ"){
			JFrame dPF = new DodajTypUprawnieniaInputFrame();
			((DodajTypUprawnieniaInputFrame) dPF).start();
		}
		
		
	}

}

	
